﻿using System;
using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web.Security;

namespace WebPages.Account
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        void SendConfirmationEmail()
        {
            var userName = Membership.GetUser().UserName;
            var newPassword = ChangeUserPassword.NewPassword;
            var emailAddress = Membership.GetUser().Email;

            // Now lets create an email message
            var emailMessage = new StringBuilder();
            var header = Resources.Email.EmailHeader.Replace("{0}", "Your MyApp account password has been changed");
            emailMessage.Append(header);
            emailMessage.Append(string.Format("Hello, {0}.<br />Your new password is: <span class=\"password\">{1}</span>", userName, newPassword));
            var footer = String.Format(Resources.Email.EmailFooter, "This e-mail was sent because someone changed a password to an account at <a href=\"http://www.MyApp.com\">www.MyApp.com</a> registered to your e-mail address.");
            emailMessage.Append(footer);

            var email = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["fromEmail"]) };
            email.To.Add(new MailAddress(emailAddress));
            email.Subject = "Your MyApp account password has been changed";
            email.Body = emailMessage.ToString();
            email.IsBodyHtml = true;

            // Send the email
            var client = new SmtpClient();
            client.Send(email);
        }

        protected void ChangeUserPassword_ChangedPassword(object sender, EventArgs e)
        {
            SendConfirmationEmail();
        }
    }
}
